﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows.Threading;

namespace MP3Ganizer.Common.BaseObject
{
    public abstract class ModuleViewModelBase: INotifyPropertyChanged
    {
        private bool _isBusy = false;

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        public bool IsBusy
        {
            get { return _isBusy; }
            protected set {
                _isBusy = value;
                OnPropertyChanged("IsBusy");
            }
        }

        public void PropertiesChanged(string[] properties)
        {
            foreach (var property in properties)
                OnPropertyChanged(property);

        }

        public void Initialize()
        {
            IsBusy = true;
            Task.Factory
                .StartNew(OnLoad)
                .ContinueWith(o =>
                    {
                        OnloadCompleted();
                        IsBusy = false;
                    },TaskScheduler.FromCurrentSynchronizationContext());
        }
        protected abstract void OnloadCompleted();
        protected abstract void OnLoad();
        
    }
}
